/*
 * ArtistPanel.java
 *
 * Created on June 11, 2005, 12:30 PM
 */
package com.bff.mpd.musicjukebox.ui;

import com.bff.mpd.musicjukebox.lib.AlbumTableModel;
import com.bff.mpd.musicjukebox.lib.ArtistTableModel;
import com.bff.mpd.musicjukebox.lib.MusicObjects;
import com.bff.mpd.musicjukebox.lib.SongTableModel;
import com.bff.bjj.ui.component.BJJPanel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.bff.javampd.MPDDatabase;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDAlbum;
import org.bff.javampd.objects.MPDArtist;

/**
 *
 * @author  Bill
 */
public class ExplorerTreePanel
        extends BJJPanel
        implements ExplorerIface {

    private DefaultMutableTreeNode topNode = new DefaultMutableTreeNode("Artist");
    private DefaultTreeModel treeModel = new DefaultTreeModel(topNode);
    private MPDDatabase database;
    private MPDPlaylist playlist;
    private SongTableModel model;
    private static final int START_DIVIDER_LOCATION = 400;

    public ExplorerTreePanel() {
    }

    /** Creates new form ArtistPanel */
    public ExplorerTreePanel(MPDDatabase database, MPDPlaylist playlist) {
        this.database = database;
        this.playlist = playlist;
        initComponents();

        artistTree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent tse) {
                nodeSelected(tse);
            }
        });
        
        if (mainSplitPane.getDividerLocation() < START_DIVIDER_LOCATION) {
            mainSplitPane.setDividerLocation(START_DIVIDER_LOCATION);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        mainSplitPane = new javax.swing.JSplitPane();
        treeScrollPane = new javax.swing.JScrollPane();
        artistTree = new javax.swing.JTree(treeModel);
        songListTablePanel = new SongListTablePanel();

        setLayout(new java.awt.BorderLayout());

        treeScrollPane.setPreferredSize(new java.awt.Dimension(200, 322));
        createNodes(topNode);
        artistTree.setShowsRootHandles(true);
        artistTree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
            public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
            }
            public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
                treeWillExpanded(evt);
            }
        });

        treeScrollPane.setViewportView(artistTree);

        mainSplitPane.setLeftComponent(treeScrollPane);

        mainSplitPane.setRightComponent(songListTablePanel);

        add(mainSplitPane, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents

    private void treeWillExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_treeWillExpanded
        MPDArtist artist = null;
        MPDAlbum album = null;

        DefaultMutableTreeNode albumNode = null;
        DefaultMutableTreeNode artistNode = null;

        try {
            createNodes(topNode);   //in case of any changes
            Object selObject = ((DefaultMutableTreeNode) evt.getPath().getPathComponent(1)).getUserObject();
            artistNode = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();

            artist = ((MPDArtist) selObject);
            List<MPDAlbum> albumList = new ArrayList<MPDAlbum>(getDatabase().listAlbumsByArtist(artist));
            artistNode.removeAllChildren();
            for (Iterator iter = albumList.iterator(); iter.hasNext();) {
                album = (MPDAlbum) iter.next();
                albumNode = new DefaultMutableTreeNode(album);
                artistNode.add(albumNode);
            }
        } catch (IllegalArgumentException iae) {
            //means first node opened so ignore
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_treeWillExpanded
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree artistTree;
    private javax.swing.JSplitPane mainSplitPane;
    private com.bff.mpd.musicjukebox.ui.SongListTablePanel songListTablePanel;
    private javax.swing.JScrollPane treeScrollPane;
    // End of variables declaration//GEN-END:variables

    private void createNodes(DefaultMutableTreeNode top) {

        DefaultMutableTreeNode artistNode = null;
        DefaultMutableTreeNode albumNode = null;
        List<MPDArtist> artistList = null;

        try {
            artistList = new ArrayList<MPDArtist>(MusicObjects.getArtists());
            /**
             * Sort the list
             */
            //java.util.Collections.sort(artistList);
            top.removeAllChildren();

            for (MPDArtist artist : artistList) {
                artistNode = new DefaultMutableTreeNode(artist);
                top.add(artistNode);
                albumNode = new DefaultMutableTreeNode("Temp");
                artistNode.add(albumNode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshComponents() {
        createNodes(topNode);
    }

    private void nodeSelected(TreeSelectionEvent tse) {
        TreePath selectedPath = artistTree.getSelectionPath();

        try {
            Object selObject =
                    ((DefaultMutableTreeNode) selectedPath.getLastPathComponent()).getUserObject();
            if (selectedPath.getPathCount() == 2) {
                MPDArtist artist = ((MPDArtist) selObject);
                model = new ArtistTableModel(artist);
            } else if (artistTree.getSelectionPath().getPathCount() == 3) {
                MPDArtist artist = (MPDArtist) (((DefaultMutableTreeNode) selectedPath.getPathComponent(1)).getUserObject());
                MPDAlbum album = ((MPDAlbum) selObject);
                model = new AlbumTableModel(artist, album);
            }
            songListTablePanel.setModel(model);
        } catch (Exception e) {
        }
    }

    /**
     * @return the database
     */
    public MPDDatabase getDatabase() {
        return database;
    }

    /**
     * @param database the database to set
     */
    public void setDatabase(MPDDatabase database) {
        this.database = database;
    }
}
